package com.md.api.service.impl;

import com.md.api.common.utils.TiandituUtil;
import com.md.api.common.utils.IdUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.common.utils.TokenUtil;
import com.md.api.dao.ReceivingAddrInfoMapper;
import com.md.api.entity.HttpResult;
import com.md.api.entity.ReceivingAddrInfo;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.service.ReceivingAddrSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @Author liujie
 * @ClassName ProductServiceImpl
 **/

@Slf4j
@Service
public class ReceivingAddrServiceImpl implements ReceivingAddrSevice {



    @Resource
    TokenUtil tokenUtil;
    @Resource
    ReceivingAddrInfoMapper addrInfoMapper;


    @Override
    public HttpResult addReceivingAddr(HttpServletRequest request, ReceivingAddrInfo addrInfo) {
        String userId = tokenUtil.getUserId(request);
        String receivingAddrId = IdUtil.uuid();
        addrInfo.setReceivingAddrId(receivingAddrId);
//        if(StringUtils.isEmpty(addrInfo.getLongitudeLatitude())) {
//            addrInfo.setReceivingAddress(addrInfo.getReceivingAddress().replace(addrInfo.getCity(),""));
//            addrInfo.setReceivingAddress(addrInfo.getReceivingAddress().replace(addrInfo.getProvince(),""));
//            addrInfo.setReceivingAddress(addrInfo.getReceivingAddress().replace(addrInfo.getCounty(),""));
//            String lonAndLatByAddress = TiandituUtil.getLonAndLatByAddress(addrInfo.getProvince()
//                    + addrInfo.getCity() + addrInfo.getCounty() + addrInfo.getReceivingAddress());
//            addrInfo.setLongitudeLatitude(lonAndLatByAddress);
//        }
        addrInfo.setUserId(userId);
        addrInfoMapper.insert(addrInfo);
        addrInfoMapper.setDefault(userId,receivingAddrId);
        return HttpResult.OK;
    }

    @Override
    public HttpResult updateReceivingAddr(HttpServletRequest request, ReceivingAddrInfo addrInfo) {

        addrInfo.setReceivingAddress(addrInfo.getReceivingAddress().replace(addrInfo.getCity(),""));
        addrInfo.setReceivingAddress(addrInfo.getReceivingAddress().replace(addrInfo.getProvince(),""));
        addrInfo.setReceivingAddress(addrInfo.getReceivingAddress().replace(addrInfo.getCounty(),""));

        String lonAndLatByAddress = TiandituUtil.getLonAndLatByAddress(addrInfo.getProvince()
                + addrInfo.getCity() + addrInfo.getCounty() + addrInfo.getReceivingAddress());
        addrInfo.setLongitudeLatitude(lonAndLatByAddress);
        addrInfoMapper.updateById(addrInfo);
        return HttpResult.OK;
    }

    @Override
    public HttpResult myAddr(HttpServletRequest request) {
        String userId = tokenUtil.getUserId(request);
//        String userId = request.getHeader("userId");
        List<ReceivingAddrInfo> addrInfos = addrInfoMapper.myAddr(userId);
        return new HttpResult(addrInfos);
    }


    @Override
    public HttpResult myDefaltAddr(HttpServletRequest request) {
        String userId = tokenUtil.getUserId(request);
        ReceivingAddrInfo addrInfo = addrInfoMapper.myDefaltAddr(userId);
        return new HttpResult(addrInfo);
    }

    @Override
    public HttpResult setDefault(HttpServletRequest request, ComposeCommitDto qto) {
        String userId = tokenUtil.getUserId(request);
//        String userId = request.getHeader("userId");
        addrInfoMapper.setDefault(userId,qto.getReceivingAddrId());
        return HttpResult.OK;
    }

    @Override
    public HttpResult removeAddr(HttpServletRequest request, ComposeCommitDto qto) {
        addrInfoMapper.deleteById(qto.getReceivingAddrId());
        return HttpResult.OK;
    }
}
